package api

import (
	"singo/service"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// DemandCreate 新增需求
// @Summary 新增需求
// @Router /api/v1/demand/create [post]
// @tags 需求相关
// @accept application/json
// @Param name query string true "需求名称"
// @Param intro query string false "介绍"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param status query int false "状态"
// @Param priority query int false "优先级"
// @Param created_by query int false "创建人"
// @Param execute_by query int false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DemandCreate(c *gin.Context) {
	var service service.DemandCreateService
	session := sessions.Default(c)
	userUID := session.Get("user_uid")

	if err := c.ShouldBind(&service); err == nil {
		res := service.DemandCreate(userUID.(string))
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DemandList 需求列表接口
// @Summary 需求列表
// @Router /api/v1/demands [get]
// @tags 需求相关
// @accept application/json
// @Param user_id query string false "关联用户ID"
// @Param organization_id query string false "关联小组ID"
// @Param project_id query string false "关联项目ID"
// @Param status query int false "状态"
// @Param pageNo query int false "页码"
// @Param pageSize query int false "条数"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DemandList(c *gin.Context) {
	service := service.ListDemandService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.DemandList()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DemandShow 需求详情接口
// @Summary 需求详情
// @Router /api/v1/demand/{id} [get]
// @tags 需求相关
// @accept application/json
// @Param id path string false "需求ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DemandShow(c *gin.Context) {
	service := service.DemandShowService{}
	res := service.DemandShow(c.Param("id"))
	c.JSON(200, res)
}

// DemandUpdate 更新需求接口
// @Summary 更新需求
// @Router /api/v1/demand/{id} [put]
// @tags 需求相关
// @accept application/json
// @Param id path string false "需求ID"
// @Param name query string false "需求名称"
// @Param intro query string false "需求介绍"
// @Param priority query int false "优先级"
// @Param status query int false "状态"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param execute_by query string false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DemandUpdate(c *gin.Context) {
	service := service.DemandUpdateService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.DemandUpdate()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// DemandDelete 删除需求接口
// @Summary 删除需求
// @Router /api/v1/demand/{id} [delete]
// @tags 需求相关
// @accept application/json
// @Param id path string false "需求ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func DemandDelete(c *gin.Context) {
	service := service.DemandDeleteService{}
	res := service.DemandDelete(c.Param("id"))
	c.JSON(200, res)
}
